﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGRapidForm.Common;
using SGRapidForm.Permission.Entity;
using FishWebLib;
using System.Collections.Specialized;
using System.Data;


namespace SGRapidForm.Permission.Component
{
    public class RoleComponent : ComponentBase
    {
        /// <summary>
        /// 新增一个角色
        /// </summary>
        /// <param name="Role"></param>
        /// <returns></returns>
        public bool Role_Add(Role Role)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Role_Add", Role) > 0);
        }

        /// <summary>
        /// 删除一个指定角色
        /// </summary>
        /// <param name="keyid"></param>
        /// <returns></returns>
        public bool Role_Delete(string keyid)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Role_Delete", null, SqlHelper.GenerateSqlParameter(keyid, System.Data.DbType.String)) > 0);
        }

        /// <summary>
        /// 批量删除角色
        /// </summary>
        /// <param name="keyids"></param>
        /// <returns></returns>
        public bool Role_BulkDelete(string keyids)
        {
            //Keyid数量
            int count = SqlHelper.GetKeyidsCount(keyids);
            //Keyids转换成Sql语句
            keyids = SqlHelper.ConvertKeyidsToSqlStatement(keyids);

            return FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Role_BulkDelete", null, keyids) > 0;
        }

        /// <summary>
        /// 更新一个指定角色
        /// </summary>
        /// <param name="Role"></param>
        /// <returns></returns>
        public bool Role_Update(Role Role)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Role_Update", Role) > 0);
        }

        /// <summary>
        /// 过滤查询角色
        /// </summary>
        /// <param name="filterConditions">Sql查询过滤条件</param>
        /// <returns></returns>
        public List<Role> Role_FilteredGetAll(string filterConditions)
        {
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "RoleCode", DbType.String, " AND RoleCode like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "RoleName", DbType.String, " AND RoleName like '{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "HelpCode", DbType.String, " AND HelpCode like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Grade", DbType.Int32, " AND Grade = '{0}' "));
            filterConditions = sql.ToString();
            return FishBLLHelper.CallSpGetDataItemList<Role>("sgrf_permission_Role_FilteredGetAll", null, filterConditions);
        }

        /// <summary>
        /// 根据Keyid查询一个角色
        /// </summary>
        /// <param name="keyid"></param>
        /// <returns></returns>
        public Role GetByKeyid(string keyid)
        {
            return FishBLLHelper.CallSpGetDataItem<Role>("sgrf_permission_Role_GetByKeyid", null, keyid);
        }

        /// <summary>
        /// 查询指定用户被分配的角色
        /// </summary>
        public List<Role> GetByUserKeyid(string userKeyid)
        {
            return FishBLLHelper.CallSpGetDataItemList<Role>("sgrf_permission_Role_GetByUserKeyid", null, userKeyid);
        }

        /// <summary>
        /// 查询指定用户未被分配的角色
        /// </summary>
        public List<Role> Role_GetUnmappedByUserKeyid(string userKeyid)
        {
            return FishBLLHelper.CallSpGetDataItemList<Role>("sgrf_permission_Role_GetUnmappedByUserKeyid", null, userKeyid);
        }

        /// <summary>
        /// 清除所有角色
        /// </summary>
        public bool Role_Clear()
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Role_Clear", null) >= 0);
        }
    }
}
